package com.iscas.tech.controller;

import com.iscas.tech.entity.AtModulevalue;
import com.iscas.tech.param.AtModulevaluePageParam;
import com.iscas.tech.service.AtModulevalueService;
import com.iscas.tech.vo.AtModulevalueQueryVo;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 项目基础信息模块值表 控制器
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/mv")
@Api(value = "项目基础信息模块值表API", tags = {"项目基础信息模块值表"})
public class AtModulevalueController extends BaseController {

    @Autowired
    private AtModulevalueService atModulevalueService;

    /**
     * 添加项目基础信息模块值表
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加项目基础信息模块值表", response = ApiResult.class)
    public ApiResult<Boolean> addAtModulevalue(AtModulevalue atModulevalue) throws Exception {
        if (LoginUtil.getUserId() == null) {
            return ApiResult.fail(ApiCode.UNAUTHORIZED);
        }
        atModulevalue.setCreatetitle(LoginUtil.getUserId().intValue());
        boolean flag = atModulevalueService.saveAtModulevalue(atModulevalue);
        return ApiResult.result(flag);
    }

    /**
     * 修改项目基础信息模块值表
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改项目基础信息模块值表", response = ApiResult.class)
    public ApiResult<Boolean> updateAtModulevalue(AtModulevalue atModulevalue) throws Exception {
        boolean flag = atModulevalueService.updateAtModulevalue(atModulevalue);
        return ApiResult.result(flag);
    }

    /**
     * 删除项目基础信息模块值表
     */
    @RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除项目基础信息模块值表", response = ApiResult.class)
    public ApiResult<Boolean> deleteAtModulevalue(String ids) throws Exception {
        String idArr[] = ids.split(",");
        for (String id : idArr) {
            atModulevalueService.deleteAtModulevalue(Long.parseLong(id));
        }

        return ApiResult.ok();
    }

    /**
     * 获取项目基础信息模块值表详情
     */
    @RequestMapping(value = "info", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "项目基础信息模块值表详情", response = AtModulevalue.class)
    public ApiResult<AtModulevalue> getAtModulevalue(Long id) throws Exception {
        AtModulevalue atModulevalue = atModulevalueService.getById(id);
        return ApiResult.ok(atModulevalue);
    }

    /**
     * 项目基础信息模块值表分页列表
     */
    @PostMapping("/getPageList")
    @ApiOperation(value = "项目基础信息模块值表分页列表", response = AtModulevalue.class)
    public ApiResult<Paging<AtModulevalueQueryVo>> getAtModulevaluePageList(AtModulevaluePageParam atModulevaluePageParam) throws Exception {
        Paging<AtModulevalueQueryVo> paging = atModulevalueService.getAtModulevaluePageList(atModulevaluePageParam);
        return ApiResult.ok(paging);
    }

    /**
     * @param mdId 模块ID
     * @return 获取模块下属所有模块值信息
     * @throws Exception 查询异常
     */
    @GetMapping("/getAllInMd")
    public ApiResult<List<AtModulevalueQueryVo>> getAllInMd(@NotNull(message = "无效参数") Long mdId) throws Exception {
        return ApiResult.ok(atModulevalueService.getAllInMd(mdId));
    }

}

